package hos.task;

/**
 * <p>Title: AutoStatus </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @version : 1.0
 * @date : 2023-02-03 10:35
 */
public class AutoStatus {

    private StatusType statusType;

    /// 获取界面状态
    public StatusType statusType() {
        return statusType;
    }

    public AutoStatus(StatusType loadState) {
        statusType = loadState;
    }

    /**
     * 成功
     */
    public static AutoStatus success() {
        return new AutoStatus(StatusType.success);
    }

    /**
     * 加载中
     */
    public static AutoStatus loading() {
        return new AutoStatus(StatusType.loading);
    }

    /**
     * 空的
     */
    public static AutoStatus empty() {
        return new AutoStatus(StatusType.noData);
    }

    /**
     * 网络异常
     */
    public static AutoStatus network() {
        return new AutoStatus(StatusType.networkConnectError);
    }

    /**
     * 异常
     */
    public static AutoStatus failed() {
        return new AutoStatus(StatusType.failed);
    }

    /**
     * 加载中
     */
    public boolean changeLoading() {
        return changeLoadStatus(StatusType.loading, null);
    }

    /**
     * 成功
     */
    public boolean changeSuccess() {
        return changeLoadStatus(StatusType.success, null);
    }

    /**
     * 空的
     */
    public boolean changeEmpty() {
        return changeLoadStatus(StatusType.noData, null);
    }

    /**
     * 网络异常
     */
    public boolean changeNetwork() {
        return changeLoadStatus(StatusType.networkConnectError, null);
    }

    /**
     * 异常
     */
    public boolean changeFailed() {
        return changeLoadStatus(StatusType.failed, null);
    }

    /// 更换界面状态
    public boolean changeLoadStatus(StatusType value) {
        return changeLoadStatus(value, null);
    }

    /**
     * 加载中
     */
    public boolean changeLoading(ChangeStatus changeStatus) {
        return changeLoadStatus(StatusType.loading, changeStatus);
    }

    /**
     * 成功
     */
    public boolean changeSuccess(ChangeStatus changeStatus) {
        return changeLoadStatus(StatusType.success, changeStatus);
    }

    /**
     * 空的
     */
    public boolean changeEmpty(ChangeStatus changeStatus) {
        return changeLoadStatus(StatusType.noData, changeStatus);
    }

    /**
     * 网络异常
     */
    public boolean changeNetwork(ChangeStatus changeStatus) {
        return changeLoadStatus(StatusType.networkConnectError, changeStatus);
    }

    /**
     * 异常
     */
    public boolean changeFailed(ChangeStatus changeStatus) {
        return changeLoadStatus(StatusType.failed, changeStatus);
    }

    /// 更换界面状态
    public boolean changeLoadStatus(StatusType value, ChangeStatus changeStatus) {
        if (isLoadCanChange(value)) {
            statusType = value;
            if (changeStatus != null) {
                changeStatus.changed(value);
            }
            return true;
        }
        return false;
    }

    /**
     * 是否和之前的一样
     */
    public boolean isLoadCanChange(StatusType value) {
        return statusType != value;
    }

    public interface ChangeStatus {

        void changed(StatusType statusType);
    }
}
